package com.zyf.controller;


import com.zyf.pojo.CheckRequest;
import com.zyf.pojo.MedicalTechnology;
import com.zyf.service.CheckRequestService;
import com.zyf.vo.CheckResultVo;
import com.zyf.vo.RegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
public class CheckRequestController {

    //注入service
    @Autowired
    private CheckRequestService checkRequestService;

    /*保存检查单数据*/
    @PostMapping("/saveCheckRequest")
    public Map<String,Object> saveCheckRequest(
            @RequestBody CheckRequest checkRequest){
        Map<String,Object> map=new HashMap<>();
        System.out.println(checkRequest);
        try{


            List<CheckRequest> requestList=new ArrayList<>();
            for(Integer tid:checkRequest.getMedicalTechnologyIdList()){
                CheckRequest target=new CheckRequest();
                //进行对象的属性拷贝,两个对象名字相同的属性进行拷贝
                BeanUtils.copyProperties(checkRequest,target);
                target.setMedicalTechnologyId(tid);
                target.setCreationTime(new Date());
                target.setCheckState("未检查");
                requestList.add(target);
            }

            checkRequestService.saveBatch(requestList);
            map.put("msg","检查申请成功....");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","检查申请失败.....");
            map.put("code",500);
        }
        return map;
    }

    /*处理加载病人检查项列表的请求*/
    /*@GetMapping("/queryCheckList/{caseNumber}/{realName}")
    public List<MedicalTechnology> queryCheckList(
            @PathVariable String caseNumber
            ,@PathVariable String realName){
        return checkRequestService.queryCheckRequestList(caseNumber,realName);

    }*/
    @GetMapping("/queryCheckList")
    public List<MedicalTechnology> queryCheckList(
            String caseNumber,
            String realName){
        return checkRequestService.queryCheckRequestList(caseNumber,realName);

    }


    /*处理支付后同时修改check_request状态的请求*/
    @PostMapping("/updateCheckPayState")
    public Map<String,Object> updatePayState(@RequestBody Integer[] checkIds){
        Map<String,Object> map=new HashMap<>();
        List<CheckRequest> checkRequestList=new ArrayList<>();
        try{
            for(Integer id:checkIds){
                CheckRequest checkRequest=new CheckRequest();
                checkRequest.setId(id);
                checkRequest.setCheckState("已结算");

                checkRequestList.add(checkRequest);
            }
            checkRequestService.updateBatchById(checkRequestList);

            map.put("msg","支付成功");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","支付失败");
            map.put("code",500);
        }
        return map;
    }

    /*处理加载待检查人员列表请求*/
    @RequestMapping("/queryNoCheckList")
    public Map<String,Object> queryNoCheckList(
            @RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "10") Integer pageSize
            ,String caseNumber
            , String realName){
        System.out.println(caseNumber);
        return checkRequestService.queryNoCheckListService(pageNum,pageSize
                ,caseNumber,realName);

    }

    /*处理加载某个病人某个检查项的请求*/
    @RequestMapping("/queryCheckItem")
    public List<MedicalTechnology> queryCheckItem(Integer id){
        MedicalTechnology medicalTechnology =
                checkRequestService.queryCheckItemListService(id);
        List<MedicalTechnology> list=new ArrayList<>();
        list.add(medicalTechnology);
        return list;
    }

    /*处理开始检查登记请求*/
    @RequestMapping("/startCheckRecord")
    public Map<String,Object> startCheck(Integer id,Integer empId){
        Map<String,Object> map=new HashMap<>();

        try{
            CheckRequest checkRequest=new CheckRequest();
            checkRequest.setId(id);
            checkRequest.setCheckEmployeeId(empId);
            checkRequest.setCheckState("检查进行中");
            checkRequest.setCheckTime(new Date());
            checkRequestService.updateById(checkRequest);

            map.put("msg","检查登记成功");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","检查登记失败");
            map.put("code",500);
        }
        return map;
    }

    /*处理加载正在检查人员列表请求*/
    @RequestMapping("/queryChecking")
    public Map<String,Object> queryChecking(
            @RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "10") Integer pageSize
            ,String caseNumber
            , String realName){
        System.out.println(caseNumber);
        return checkRequestService.queryCheckingListService(pageNum,pageSize
                ,caseNumber,realName);

    }

    @RequestMapping("/saveCheckResult")
    public Map<String,Object> saveCheckResult(@RequestBody CheckResultVo checkResultVo){
        Map<String,Object> map=new HashMap<>();

        try{
            CheckRequest checkRequest=new CheckRequest();
            checkRequest.setId(checkResultVo.getId());
            checkRequest.setInputcheckEmployeeId(checkResultVo.getEmpId());
            checkRequest.setCheckState("已完成检查");
            checkRequest.setCheckResult(checkResultVo.getCheckResult());
            checkRequestService.updateById(checkRequest);

            map.put("msg","检查结果保存成功");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","检查结果保存失败");
            map.put("code",500);
        }
        return map;
    }


    /*处理加载某个病人已经完成检查项列表*/
    @GetMapping("/queryCheckedList/{id}")
    public List<MedicalTechnology> queryCheckedList(@PathVariable Integer id){
        return checkRequestService.queryCheckedRequestList(id);
    }

    /*处理查询检查结果请求*/
    @GetMapping("/queryCheckResult/{id}")
    public CheckRequest queryCheckResult(@PathVariable Integer id){
        return checkRequestService.queryCheckResultService(id);
    }



}
